package com.zcb.qingshoudan.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.zcb.qingshoudan.R;
import com.zcb.qingshoudan.fragment.GatheringFragment;
import com.zcb.qingshoudan.fragment.HomePageFragment;
import com.zcb.qingshoudan.fragment.IFragment;
import com.zcb.qingshoudan.fragment.RefundFragment;
import com.zcb.qingshoudan.gridview.MyGridView;

public class MainActivity extends Activity implements View.OnClickListener,IFragment{
    private MyGridView gridview;
    public static boolean b = false;
    private Fragment mFragment;

    //title main
    private TextView titleTV;
    private LinearLayout titleLinearLayout;

    //bottom bar
    private LinearLayout activityMainBottom;
    private LinearLayout activityMainBottom1;
    private LinearLayout activityMainBottom2;
    private LinearLayout activityMainBottom3;
    private LinearLayout activityMainBottom4;
    private ImageView activityMainBottomImg1;
    private ImageView activityMainBottomImg2;
    private ImageView activityMainBottomImg3;
    private ImageView activityMainBottomImg4;
    private TextView activityMainBottomTxt1;
    private TextView activityMainBottomTxt2;
    private TextView activityMainBottomTxt3;
    private TextView activityMainBottomTxt4;

//    private List<BaseFragment> t;
    private HomePageFragment homePageFragment;
    private GatheringFragment gatheringFragment;
    private RefundFragment refundFragment;
//    private VerificationFragment x;
//    private BaseFragment y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initView(){


    //title main
    titleLinearLayout= (LinearLayout) findViewById(R.id.title_lin_back);
    titleTV= (TextView) findViewById(R.id.title_text);
    titleTV.setText("收单啦");

    //bottom Bar
    activityMainBottom= (LinearLayout) findViewById(R.id.activity_main_bottom);
    activityMainBottom1= (LinearLayout) findViewById(R.id.activity_main_bottom_01);
    activityMainBottom2= (LinearLayout) findViewById(R.id.activity_main_bottom_02);
    activityMainBottom3= (LinearLayout) findViewById(R.id.activity_main_bottom_03);
    activityMainBottom4= (LinearLayout) findViewById(R.id.activity_main_bottom_04);
    activityMainBottomImg1= (ImageView) findViewById(R.id.activity_main_bottom_01_image);
    activityMainBottomImg2= (ImageView) findViewById(R.id.activity_main_bottom_02_image);
    activityMainBottomImg3= (ImageView) findViewById(R.id.activity_main_bottom_03_image);
    activityMainBottomImg4= (ImageView) findViewById(R.id.activity_main_bottom_04_image);
    activityMainBottomTxt1= (TextView) findViewById(R.id.activity_main_bottom_01_text);
    activityMainBottomTxt2= (TextView) findViewById(R.id.activity_main_bottom_02_text);
    activityMainBottomTxt3= (TextView) findViewById(R.id.activity_main_bottom_03_text);
    activityMainBottomTxt4= (TextView) findViewById(R.id.activity_main_bottom_04_text);

        activityMainBottom1.setOnClickListener(this);
        activityMainBottom2.setOnClickListener(this);
        activityMainBottom3.setOnClickListener(this);
        activityMainBottom4.setOnClickListener(this);
    //init fragment
        homePageFragment=new HomePageFragment(this,this);
        gatheringFragment=new GatheringFragment(this,this);
        refundFragment=new RefundFragment(this,this);

    }

    private void initEvent(){
        FragmentManager fragmentManager=getFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.mContains,homePageFragment);
//        fragmentTransaction.addToBackStack(null);//是否加入到返回栈中
        fragmentTransaction.commit();
        mFragment=homePageFragment;
    }

    private void switchFragment(int rid){
//        Toast.makeText(this,"id:"+rid,Toast.LENGTH_SHORT).show();
        //开始扫描二维码
        startActivityForResult(new Intent(this, CaptureActivity.class),0);
    }
    public void switchContent(Fragment from, Fragment to) {
        if(mFragment!=to) {
            mFragment = to;
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction()
                    .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(from).add(R.id.mContains, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }

        }

    }
    /**
     * 扫描结到结果后显示出来==================================
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            Bundle bundle = data.getExtras();
            if (bundle != null) {
                String result=bundle.getString("result");
                Toast.makeText(MainActivity.this,"result:"+result,Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.activity_main_bottom_01 :
                titleTV.setText("请收单");
                activityMainBottomImg1.setImageResource(R.drawable.icon_home_d);
                activityMainBottomImg2.setImageResource(R.drawable.icon_gathering_u);
                activityMainBottomImg3.setImageResource(R.drawable.icon_refund_u);
                activityMainBottomImg4.setImageResource(R.drawable.icon_check_u);
                activityMainBottomTxt1.setTextColor(getResources().getColor(R.color.title_bg));
                activityMainBottomTxt2.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                activityMainBottomTxt3.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                activityMainBottomTxt4.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                switchContent(mFragment,homePageFragment);
                break;
            case R.id.activity_main_bottom_02 :
                titleTV.setText("立即收款");
                activityMainBottomImg1.setImageResource(R.drawable.icon_home_u);
                activityMainBottomImg2.setImageResource(R.drawable.icon_gathering_d);
                activityMainBottomImg3.setImageResource(R.drawable.icon_refund_u);
                activityMainBottomImg4.setImageResource(R.drawable.icon_check_u);
                activityMainBottomTxt2.setTextColor(getResources().getColor(R.color.title_bg));
                activityMainBottomTxt1.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                activityMainBottomTxt3.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                activityMainBottomTxt4.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                switchContent(mFragment,gatheringFragment);
                break;
            case R.id.activity_main_bottom_03 :
                titleTV.setText("请付款");
                activityMainBottomImg1.setImageResource(R.drawable.icon_home_u);
                activityMainBottomImg2.setImageResource(R.drawable.icon_gathering_u);
                activityMainBottomImg3.setImageResource(R.drawable.icon_refund_d);
                activityMainBottomImg4.setImageResource(R.drawable.icon_check_u);
                activityMainBottomTxt3.setTextColor(getResources().getColor(R.color.title_bg));
                activityMainBottomTxt2.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                activityMainBottomTxt1.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                activityMainBottomTxt4.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                switchContent(mFragment,refundFragment);
                break;
            case R.id.activity_main_bottom_04 :
                switchFragment(4);
                activityMainBottomImg1.setImageResource(R.drawable.icon_home_u);
                activityMainBottomImg2.setImageResource(R.drawable.icon_gathering_u);
                activityMainBottomImg3.setImageResource(R.drawable.icon_refund_u);
                activityMainBottomImg4.setImageResource(R.drawable.icon_check_d);
                activityMainBottomTxt4.setTextColor(getResources().getColor(R.color.title_bg));
                activityMainBottomTxt2.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                activityMainBottomTxt3.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                activityMainBottomTxt1.setTextColor(getResources().getColor(R.color.main_buttom_text_up));
                break;
        }
    }

    @Override
    public void openAty(int paramInt) {
        switch (paramInt){
            case 0://微信收钱
                Intent intent=new Intent(this, GatheringActivity.class);
                intent.putExtra("shangjia","wx");
                startActivity(intent);
                break;
            case 1://支付宝收钱
                Intent intent1=new Intent(this, GatheringActivity.class);
                intent1.putExtra("shangjia","zfb");
                startActivity(intent1);
                break;
            case 3://卡券核销
                switchFragment(4);
                break;
            case 4://收款记录
                Intent intent4=new Intent(this, GatheringHistoryActivity.class);
                intent4.putExtra("shangjia","zfb");
                startActivity(intent4);
                break;
            case 5://核销记录
                Intent intent5=new Intent(this, VerificationHistoryActivity.class);
                intent5.putExtra("shangjia","zfb");
                startActivity(intent5);
                break;
            case 6://收款统计
                Intent intent6=new Intent(this, GatheringCountActivity.class);
                intent6.putExtra("shangjia","zfb");
                startActivity(intent6);
                break;
            case 7://商户信息
                Intent intent7=new Intent(this, MerchantsMsgActivity.class);
                intent7.putExtra("shangjia","zfb");
                startActivity(intent7);
                break;
            case 8://修改密码
                Intent intent8=new Intent(this, ModifyPassWordActivity.class);
                intent8.putExtra("shangjia","zfb");
                startActivity(intent8);
                break;
        }
    }

    @Override
    public void openAty(Intent intent) {
        startActivity(intent);
    }

    //最后一个fragment，按返回键，弹出退出提示框
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            if (this.getFragmentManager().getBackStackEntryCount()==0){
                new AlertDialog.Builder(this).setMessage("退出程序？").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                  .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          finish();
                      }
                  })
                    .create().show();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
















}
